package com.kye.express.business.waybill_search;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.waybill.waybill_detail.WayBillDetailActivity;
import com.kye.express.databinding.ItemWaybillSearchBinding;
import com.kye.express.utils.DateTimeUtils;
import com.kye.model.bean.waybill_search.WaybillHistoryModel;

import io.reactivex.functions.Consumer;

class WaybillSearchItemViewBinder extends me.drakeet.multitype.ItemViewBinder<WaybillHistoryModel, WaybillSearchItemViewBinder.ViewHolder> {

    private Context mContext;

    public WaybillSearchItemViewBinder(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_waybill_search, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull WaybillHistoryModel item) {
        holder.bindData(item);
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        private WaybillHistoryModel mWaybillHistoryModel;
        ItemWaybillSearchBinding mBinding;

        public ViewHolder(View itemView) {
            super(itemView);
            mBinding = DataBindingUtil.bind(itemView);
        }

        public void bindData(WaybillHistoryModel item) {
            mWaybillHistoryModel = item;
            if (mWaybillHistoryModel != null) {
                mBinding.setDatas(mWaybillHistoryModel);
                if (item.getTime() > 0) {
                    mBinding.tvTime.setText(DateTimeUtils.getStringDate(item.getTime()));
                }
                RxView.clicks(mBinding.getRoot())
                        .compose(new DebounceObservableTransformer<>())
                        .subscribe(new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Exception {
                                WayBillDetailActivity.startActivity(mContext,item.getNumber());
                            }
                        });
            }
        }
    }
}
